package com.mybatis.controller;

import com.mybatis.mapper.DepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.pojo.Depart;

import java.util.List;

/**
 * @author cyt
 * @create 2020-03-13 14:01
 */
@RestController
public class Controller {

    @Autowired
    private DepartMapper departMapper;

    @Autowired
    private DiscoveryClient client;


    @ResponseBody
    @RequestMapping("/getAll")
    public List<Depart> getAll() {
        return departMapper.selectByExample(null);
    }


    /**
     * 得到注册过来的微服务信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getClient")
    public Object discoveryclient() {
        List<String> services = client.getServices();
        System.out.println("client server->>>" + services);
        // 通过微服务的id 得到具体的信息
        List<ServiceInstance> instances = client.getInstances("APPLICATION-PROVIDER");
        System.out.println("instance num: " + instances.size());
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                            instance.getInstanceId() + "\t" +
                            instance.getServiceId() + "\t" +
                            instance.getPort() + "\t" +
                            instance.getUri() + "\t"
            );
        }
        return this.client;

    }

}
